using System;
using System.Net;

// Project Server 2007 References
using Microsoft.Office.Project.Server.Library;

namespace Microsoft.EPM.TSAutoStatus
{
    class ResourcePSI
    {
        #region Properties
        private string name = string.Empty;
        private bool isWindowsUser = false;
        private string windowsAccount = string.Empty;

        /// <summary>
        /// Resource namne
        /// </summary>
        public string Name
        {
            get { return name; }
        }
        /// <summary>
        /// Is windows account?
        /// </summary>
        public bool IsWindowsUser
        {
            get { return isWindowsUser; }
        }
        /// <summary>
        /// Windows account
        /// </summary>
        public string WindowsAccount
        {
            get { return windowsAccount; }
        }
        #endregion

        #region Constructors

        /// <summary>
        /// Resource constructor
        /// </summary>
        /// <param name="resourceDS"></param>
        public ResourcePSI(ResourceWS.ResourceDataSet resourceDS)
        {
            if (resourceDS.Resources.Count == 1)
            {
                name = resourceDS.Resources[0].RES_NAME;
                isWindowsUser = resourceDS.Resources[0].RES_IS_WINDOWS_USER;
                windowsAccount = resourceDS.Resources[0].IsWRES_ACCOUNTNull() ? string.Empty : resourceDS.Resources[0].WRES_ACCOUNT;
            }
        }

        #endregion
    }

    /// <summary>
    /// Perfom web service impersonation
    /// </summary>
    class ResourceDerived : ResourceWS.Resource
    {
        private String contextString = String.Empty;

        protected override WebRequest GetWebRequest(Uri uri)
        {
            //here we are overriding the GetWebRequest method and adding the 2 web request headers
            WebRequest webRequest = base.GetWebRequest(uri);
            if (contextString != String.Empty)
            {
                webRequest.UseDefaultCredentials = true;
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                webRequest.Headers.Add("PjAuth", contextString);
                webRequest.Headers.Add("ForwardFrom", "/_vti_bin/psi/resource.asmx");
                webRequest.PreAuthenticate = true;
            }
            return webRequest;
        }

        public void SetImpersonationContext(bool isWindowsUser, String userNTAccount, Guid userGuid, Guid trackingGuid, Guid siteId, String lcid)
        {
            contextString = GetImpersonationContext(isWindowsUser, userNTAccount, userGuid, trackingGuid, siteId, lcid);
        }

        private String GetImpersonationContext(bool isWindowsUser, String userNTAccount, Guid userGuid, Guid trackingGuid, Guid siteId, String lcid)
        {
            PSContextInfo contextInfo = new PSContextInfo(isWindowsUser, userNTAccount, userGuid, trackingGuid, siteId, lcid);
            return PSContextInfo.SerializeToString(contextInfo);
        }
    }
}
